﻿// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="EnsurePropertyBagItems.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines an Action to create a new site Collection.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace Collaboris.Saf.Actions.Wss.PropertyBag
{
    using System.Collections.Generic;

    using Engine;

    using Entities;

    using SharePoint.Wss.Attributes;
    using SharePoint.Wss.Farm;
    using SharePoint.Wss.WebApplications;
    using SharePoint.Wss.Webs;

    [CollaborisClass(
        Title = "Ensures Property Bag Items.",
        ReleaseStatus = ReleaseStatus.Beta,
        DisableDocumentation = false,
        ContactAuthorEmail = "Support@Collaboris.co.uk",
        Summary = "Adds (or updates) a value in the relevant property bag. " +
                  "The web, site, web application and farm property bags can all be updated." ,
        Remarks = "Please ensure that the account running the action has the permissions to " +
                  "update the property bag.",
          ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C655}")]
    public class EnsurePropertyBagItems : WSSActionBase<PropertyBagInfo>
    {
        /// <summary>
        /// Does the action.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        protected override ProcessStatus DoAction(SafContext context, PropertyBagInfo entity)
        {
            this.Trace.TraceMethodStart("DoAction");

            foreach (KeyValuePair<string, object> keyValuePair in entity.KeyValuePairs)
            {
                switch (entity.Scope)
                {
                    case SPScope.Web:
                        SPWebHelper.AddProperty(TargetWeb, keyValuePair.Key, keyValuePair.Value);
                        break;

                    case SPScope.Site:
                        SPWebHelper.AddProperty(TargetWeb.Site.RootWeb, keyValuePair.Key, keyValuePair.Value);
                        break;

                    case SPScope.WebApplication:
                        SPWebApplicationHelper.AddProperty(TargetWeb.Site.WebApplication, keyValuePair.Key, keyValuePair.Value);
                        break;

                    case SPScope.Farm:
                        SPFarmHelper.AddProperty(TargetWeb.Site.WebApplication.Farm, keyValuePair.Key, keyValuePair.Value);
                        break;
                }
            }

            this.Trace.TraceMethodEnd("DoAction");

            return ProcessStatus.Success;
        }
        
        
        /// <summary>
        /// Always deletes the Site as specified by the entity.Url
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        protected override ProcessStatus UndoAction(SafContext context, PropertyBagInfo entity)
        {
            this.Trace.TraceMethodStart("UndoAction");
            
            
            this.Trace.TraceMethodEnd("UndoAction");

            return ProcessStatus.Success;
        }

        /// <summary>
        /// Checks the Data entity used by the Action for Validity
        /// </summary>
        /// <param name="context">The <see cref="SafContext"/> passed in by the SAF Engine.</param>
        /// <param name="entity">The data entity to Validate.</param>
        /// <param name="errors">List of <see cref="ValidationError"/>s</param>
        /// <param name="mode"><see cref="ProcessMode"/>signalling wheter we are Doing, or Undoing the action.</param>
        /// <returns>false if invalid; otherwise true.</returns>
        protected override bool IsValid(
            SafContext context,
            PropertyBagInfo entity,
            ref List<ValidationError> errors,
            ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            ValidateObjectForNull(entity.KeyValuePairs, "PropertyBagInfo.KeyValuePairs", ref errors);

            // Have we added errors ?
            return errors.Count == currentCount;
        }
    }

   
}